package com.vanke.vms.management.dao.daoImpl;

import java.util.ArrayList;
import java.util.List;

import com.vanke.vms.common.domain.AddInfo;
import com.vanke.vms.common.domain.Info;
import com.vanke.vms.common.domain.Pair;
import com.vanke.vms.management.dao.AddInfoDao;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class AddInfoDaoImpl extends HibernateDaoSupport implements AddInfoDao {

	// @SuppressWarnings("deprecation")
	// get the addtril list
	public List<Pair> addiList() {
		SessionFactory sessionfactory = getHibernateTemplate()
				.getSessionFactory();
		Session session = sessionfactory.openSession();// sessionfactory.getCurrentSession();//error
		String sql = "select ad.pname,ad.cname from vms.addctrl as ad";
		// List<Object> list = session.createSQLQuery(sql).list();
		List<Object[]> list = session.createSQLQuery(sql).addScalar("pname",
				Hibernate.STRING).addScalar("cname", Hibernate.STRING).list();
		session.close();
		List<Pair> plist = new ArrayList<Pair>();
		for (int i = 0; i < list.size(); i++) {
			Object[] o = list.get(i);
			Pair tmpPair = new Pair((String) o[0], (String) o[1]);
			plist.add(tmpPair);
		}
		return plist;
	}

	public String addInfo(Info info) {
		String hql = "";
		return getHibernateTemplate().save(info).toString();
	}

	public void addAddInfo(String id, List<Pair> pairList) {
		for (int i = 0; i < pairList.size(); i++) {
			if (pairList.get(i).getValue() != null
					&& !pairList.get(i).getValue().equals("")) {
				AddInfo addInfo = new AddInfo();
				addInfo.setInfoId(Integer.parseInt(id));
				addInfo.setPname(pairList.get(i).getPname());
				addInfo.setCname(pairList.get(i).getCname());
				addInfo.setContent(pairList.get(i).getValue());
				getHibernateTemplate().persist(addInfo);
			}
		}
	}

	@Override
	public void registeInfo(Info info) {
		getHibernateTemplate().persist(info);
	}

	@Override
	public void updateInfo(Info info) {

	}

	@Override
	public boolean isDublipcated(String houseSet, String houseBuild,
			String houseNum) {
		if (getHibernateTemplate().find(
				"from Info as info where info.houseSet = '" + houseSet
						+ "' and info.houseBuild = '" + houseBuild
						+ "' and info.houseNumber = '" + houseNum + "'").size() != 0)
			return true;
		else
			return false;
	}
}
